package com.situ.mvc.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.session.SqlSession;

import com.situ.mvc.common.ServerResponse;
import com.situ.mvc.dao.IDepartmentDao;
import com.situ.mvc.dao.impl.DepartmentDaoImpl;
import com.situ.mvc.entity.Department;
import com.situ.mvc.service.IDepartmentService;
import com.situ.mvc.util.MyBatisUtil;

public class DepartmentServiceImpl implements IDepartmentService {
	
	private IDepartmentDao departmentDao;
	SqlSession sqlSession;
	
	public DepartmentServiceImpl() {
		sqlSession = MyBatisUtil.getSqlSession();
		departmentDao = sqlSession.getMapper(IDepartmentDao.class);
	}

	@Override
	public ServerResponse deleteAll(String ids) {
		String[] array = ids.split(",");
		for (String id : array) {
			departmentDao.deleteDepartmentById(Integer.parseInt(id));
		}
		return ServerResponse.createSuccess("删除成功");
	}

	@Override
	public ServerResponse addDepartment(Department department) {
		int count = departmentDao.addDepartment(department);
		if (count <= 0) {
			return ServerResponse.createError("新增失败");
		}
		return ServerResponse.createSuccess("新增成功");
	}
	//获得部门列表
	@Override
	public ServerResponse getDepartmentList(Integer page, Integer limit) {
		//获得部门的数量
		int count = departmentDao.findDepartmentCount();
		int offset = (page-1)*limit;
		Map<String, Object> map = new HashMap<>();
		map.put("offset", offset);
		map.put("limit", limit);
		List<Department> data = departmentDao.getDepartmentList(map);
		if (null == data) {
			return ServerResponse.createError("获取失败");
		}
		return ServerResponse.createSuccess("获取成功", count, data);
	}

}
